<?php

use common\models\c2\entity\Product;
use unclead\multipleinput\MultipleInput;
use yii\helpers\Html;
use kartik\widgets\ActiveForm;
use kartik\builder\Form;
use kzo\base\models\statics\EntityModelStatus;
use kzo\base\widgets\ui\bs4\Alert;
use yii\helpers\Url;

/* @var $model common\models\c2\entity\WarehouseNote */

$regularLangName = \Yii::$app->kzoHelper->getRegularLangName();
$messageName = $model->getMessageName();
?>

<?php
$form = ActiveForm::begin([
    'action' => ['edit', 'id' => $model->id],
    'options' => [
        'id' => $model->getBaseFormName(),
        'data-pjax' => true,
    ]]);
?>

<div class="<?= $model->getPrefixName('form') ?> card bg-light mt-2">
    <div class="card-body">

        <?php if (Yii::$app->session->hasFlash($messageName)): ?>
            <?php if (!$model->hasErrors()) {
                echo Alert::widget([
                    'type' => Alert::TYPE_SUCCESS,
                    'title' => 'Well Down',
                    'icon' => 'fas fa-ok-circle',
                    'body' => Yii::$app->session->getFlash($messageName),
                    'showSeparator' => true,
                    'delay' => 2000
                ]);
            } else {
                echo Alert::widget([
                    'type' => Alert::TYPE_WARNING,
                    'title' => 'Warning!',
                    'icon' => 'fas fa-ok-circle',
                    'body' => Yii::$app->session->getFlash($messageName),
                    'showSeparator' => true,
                ]);
            }
            ?>
        <?php endif; ?>

        <?php
        echo Form::widget([
            'model' => $model,
            'form' => $form,
            'columns' => 4,
            'attributes' => [
                'type' => ['type' => Form::INPUT_DROPDOWN_LIST, 'items' => \common\models\c2\statics\WarehouseNoteType::getHashMap('id', 'label')],
                'ref_note_id' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => $model->getAttributeLabel('ref_note_id'), 'readonly' => true]],
                'label' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => $model->getAttributeLabel('label')]],
                'warehouse_id' => ['type' => Form::INPUT_DROPDOWN_LIST, 'items' => \common\models\c2\entity\Warehouse::getHashMap('id', 'name')],
                'receiver_name' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => $model->getAttributeLabel('receiver_name')]],

                // 'state' => ['type' => Form::INPUT_WIDGET, 'widgetClass' => '\kartik\checkbox\CheckboxX', 'options' => [
                //     'pluginOptions' => ['threeState' => false],
                // ],],
                // 'status' => ['type' => Form::INPUT_DROPDOWN_LIST, 'items' => EntityModelStatus::getHashMap('id', 'label')],
                'position' => ['type' => Form::INPUT_WIDGET, 'widgetClass' => '\kartik\touchspin\TouchSpin',],
            ]]);
        echo Form::widget([
            'model' => $model,
            'form' => $form,
            'columns' => 1,
            'attributes' => [
                'memo' => ['type' => Form::INPUT_WIDGET, 'widgetClass' => '\vova07\imperavi\Widget', 'options' => [
                    'settings' => [
                        'minHeight' => 150,
                        'buttonSource' => true,
                        'lang' => $regularLangName,
                        'plugins' => [
                            'fontsize',
                            'fontfamily',
                            'fontcolor',
                            'table',
                            'textdirection',
                            'fullscreen',
                        ],
                    ]
                ],],
            ]
        ]);

        $multipleItemsId = $model->getPrefixName('items');
        echo Form::widget([
            'model' => $model,
            'form' => $form,
            'columns' => 1,
            'attributes' => [
                'items' => [
                    'type' => Form::INPUT_WIDGET,
                    'widgetClass' => unclead\multipleinput\MultipleInput::className(),
                    'options' => [
                        'id' => $multipleItemsId,
                        'data' => $model->items,
                        //                        'max' => 4,
                        'allowEmptyList' => true,
                        'rowOptions' => function ($model, $index, $context) use ($multipleItemsId) {
                            return ['id' => "row{multiple_index_{$multipleItemsId}}", 'data-id' => $model['id']];
                        },
                        'addButtonOptions' => [
                            'class' => 'btn btn-success',
                            'label' => Html::tag('i', null, ['class' => 'fa fa-plus'])
                        ],
                        'removeButtonOptions' => [
                            'class' => 'btn btn-danger',
                            'label' => Html::tag('i', null, ['class' => 'fa fa-minus'])
                        ],
                        'columns' => [
                            [
                                'name' => 'id',
                                'type' => 'hiddenInput',
                            ],
                            [
                                'name' => 'product_id',
                                'type' => \kartik\widgets\Select2::className(),
                                'title' => Yii::t('app.c2', 'Product'),
                                'enableError' => true,
                                'headerOptions' => ['style' => 'width: 200px',],
                                // 'items' => ['' => Yii::t("app.c2", "Select...")] + Product::getHashMap('id', 'name', ['status' => EntityModelStatus::STATUS_ACTIVE]),
                                'options' => [
                                    'data' => ['' => Yii::t("app.c2", "Select...")] + Product::getHashMap('id', 'name', [
                                            'status' => EntityModelStatus::STATUS_ACTIVE
                                        ]),
                                ],
                            ],
                            [
                                'name' => 'measure_id',
                                'title' => Yii::t('app.c2', 'Measure'),
                                'type' => 'dropDownList',
                                'headerOptions' => ['style' => 'width: 90px',],
                                'enableError' => true,
                                'items' => \common\models\c2\entity\Measure::getHashMap('id', 'label'),
                            ],
                            [
                                'name' => 'pieces',
                                // 'type' => kartik\widgets\TouchSpin::className(),
                                'title' => Yii::t('app.c2', 'Pieces'),
                                'defaultValue' => 1,
                                'options' => [

                                ]
                            ],
                            [
                                'name' => 'quantity',
                                // 'type' => kartik\widgets\TouchSpin::className(),
                                'title' => Yii::t('app.c2', 'Quantity'),
                                'defaultValue' => 0,
                                'options' => [
                                    'pluginOptions' => [
                                        // 'buttondown_txt' => '<i class="glyphicon glyphicon-minus-sign"></i>',
                                        // 'buttonup_txt' => '<i class="glyphicon glyphicon-plus-sign"></i>',
                                    ],
                                ]
                            ],
                            [
                                'name' => 'memo',
                                'title' => Yii::t('app.c2', 'Memo'),
                                'enableError' => true,
                            ],
                        ]
                    ],
                ],
            ]
        ]);

        echo Html::submitButton('<i class="fa fa-save"></i> ' . Yii::t('app.c2', 'Save'), ['type' => 'button', 'class' => 'btn btn-primary float-right ml-2']);
        echo Html::a('<i class="fa fa-arrow-left"></i> ' . Yii::t('app.c2', 'Go Back'), ['index'], ['data-pjax' => '0', 'class' => 'btn btn-light float-right', 'title' => Yii::t('app.c2', 'Go Back'),]);
        ?>

    </div>
</div>
<?php ActiveForm::end(); ?>
<?php
$js = "";
$js .= "jQuery('.btn.multiple-input-list__btn.js-input-remove').off('click').on('click', function(){
    var itemId = $(this).closest('tr').data('id');
    if(itemId){
       $.ajax({url:'" . Url::toRoute('/receipt-note/input/delete-subitem') . "',data:{id:itemId}}).done(function(result){;}).fail(function(result){alert(result);});
    }
});\n";
$this->registerJs($js);
?>